## Process this file with automake to produce Makefile.in
# Copyright (C) 2000-2012 Free Software Foundation, Inc.
# Copyright (C) 2020 Maxime Devos
#
# Author (GnuTLS): Nikos Mavrogiannopoulos
# Author (rehash): Maxime Devos
#
# This file is part of GnuTLS.
#
# rehash is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# rehash is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rehash.  If not, see <http://www.gnu.org/licenses/>.

# This file is partially based upon guile/Makefile.am in GnuTLS.

ACLOCAL_AMFLAGS = -I m4
AM_CPPFLAGS = -I$(top_srcdir)/include
SUFFIXES = .scm .go .html .pdf .ps

libexec_PROGRAMS = \
  rehash-service

rehash_service_CFLAGS = $(GNUNETUTIL_CFLAGS) $(GNUNETDATASTORE_CFLAGS) \
  $(GNUNETDHT_CFLAGS) -I$(top_srcdir)/include
rehash_service_LDFLAGS = $(GNUNETUTIL_LDFLAGS) $(GNUNETDATASTORE_LDFLAGS) \
  $(GNUNETDHT_LDFLAGS) -no-undefined
rehash_service_LDADD = $(GNUNETUTIL_LIBS) $(GNUNETDATASTORE_LIBS) \
  $(GNUNETDHT_LIBS) librehash.la
rehash_service_SOURCES = c-code/rehash-service.c c

# that's lib-rehash, not libre-hash
lib_LTLIBRARIES = librehash.la

# gnunet headers depend on libextractor headers

librehash_la_CFLAGS = $(GNUNETUTIL_CFLAGS) $(EXTRACTOR_CFLAGS)
librehash_la_LDFLAGS = $(GNUNETUTIL_LDFLAGS) -no-undefined
librehash_la_LIBADD = $(GNUNETUTIL_LIBS)
librehash_la_SOURCES = c-code/rehash-api.c \
  c-code/rehash_dht.c \
  c-code/rehash-crypto-from-data.c \
  c-code/rehash-crypto-to-data.c \
  c-code/rehash-crypto-from-hash.c

scheme-tests/nonetconf.conf: scheme-tests/nonetconf.conf.in
	$(SED) -e 's,[@]rehash_binary@,$(abs_builddir)/rehash-service,g' \
	  "$<" > "$@.tmp" && mv "$@.tmp" "$@"
%.scm: %.scmguix
	$(SED) -e 's/guix /remirror /g' "$<" > "$@.tmp" \
	  && mv "$@.tmp" "$@"

# Scheme code
modules_rehash = \
  scheme-binding/rehash/binding-utils.scm \
  scheme-binding/rehash/common.scm \
  scheme-binding/rehash/configuration.scm \
  scheme-binding/rehash/fibers.scm \
  scheme-binding/rehash/fs.scm \
  scheme-binding/rehash/operations.scm

modules_remirror = \
  scheme-binding/remirror/recognise-hash-uri.scm \
  scheme-binding/remirror/snakeoil.scm

# Not used at runtime
# scheme-binding/remirror/embed-file.scm

modules_rehash_generated = \
  scheme-binding/rehash/config.scm
modules_remirror_generated = \
  scheme-binding/remirror/base16.scm \
  scheme-binding/remirror/base32.scm
modules_toplevel = \
  scheme-binding/rehash.scm

# Install Scheme source code
if HAVE_GUILE
guilesiterehashdir = $(guilesitedir)/rehash
guilesiteremirrordir = $(guilesitedir)/remirror

# rehash
nodist_guilesiterehash_DATA = $(modules_rehash_generated)
dist_guilesiterehash_DATA = $(modules_rehash) scheme-binding/rehash/config.scm.in
# remirror
nodist_guilesiteremirror_DATA = $(modules_remirror_generated)
dist_guilesiteremirror_DATA = $(modules_remirror) \
  scheme-binding/remirror/base16.scmguix \
  scheme-binding/remirror/base32.scmguix
endif

# Scheme code compilation
if HAVE_GUILD
guilesiteccache_rehashdir = $(guilesiteccachedir)/rehash
guilesiteccache_remirrordir = $(guilesiteccachedir)/remirror
nodist_guilesiteccache_DATA = $(modules_toplevel:%.scm=%.go)

# rehash
nodist_guilesiteccache_rehash_DATA = \
  $(modules_rehash:%.scm=%.go) \
  scheme-binding/rehash/config.go

# remirror
nodist_guilesiteccache_remirror_DATA = \
  $(modules_remirror:%.scm=%.go) \
  $(modules_remirror_generated:%.scm=%.go)

# Unset 'GUILE_LOAD_COMPILED_PATH' so we can be sure that any .go file that we
# load comes from the build directory.
# XXX: Use the C locale for when Guile lacks
# <https://git.sv.gnu.org/cgit/guile.git/commit/?h=stable-2.0&id=e2c6bf3866d1186c60bacfbd4fe5037087ee5e3f>.
%.go: %.scm
	$(AM_V_GUILEC)$(MKDIR_P) "`dirname "$@"`" ;			\
	$(AM_V_P) && out=1 || out=- ;					\
	unset GUILE_LOAD_COMPILED_PATH ; LC_ALL=C			\
	builddir="$(top_builddir)"					\
	GUILE_AUTO_COMPILE=0						\
	$(GUILD) compile --target="$(host)"				\
	  -L "$(top_builddir)/scheme-binding"				\
	  -L "$(top_srcdir)/scheme-binding"				\
	  -Wformat -Wunbound-variable -Warity-mismatch			\
	  -o "$@" "$<" >&$$out

# Dependency information
# TODO generate automatically
include dependencies.make
endif

TEST_EXTENSIONS = .scm
TESTS =

# Scheme tests
# FIXME ./pre-inst-env & ./test-env
if HAVE_GUILE
SCM_LOG_DRIVER = \
  $(GUILE) -L $(top_srcdir)/scheme-binding \
    -L $(top_srcdir)/scheme-tests \
    -L $(top_srcdir) \
    -e main $(top_srcdir)/build-aux/test-driver.scm

SCM_TESTS = \
  scheme-tests/operation-store.scm \
  scheme-tests/recognise-url.scm

SCM_TESTS_ENVIRONMENT = \
  GUILE_AUTO_COMPILE=0
  GUILE_WARN_DEPRECATED=detailed
TESTS += $(SCM_TESTS)
endif

# Snakeoil certificates
snakeoil-cert.pem: snakeoil-key.pem snakeoil-templ
	certtool --no-text -s --template="$(top_srcdir)/snakeoil-templ" \
	--load-privkey="$<" --seed=deadbeef --outfile="$@.tmp" && \
	mv -T "$@.tmp" "$@"

snakeoil-key.pem:
	certtool --no-text -p --seed=deadbeef --outfile="$@.tmp" && \
	mv -T "$@.tmp" "$@"

# Documentation
nodist_html_DATA = doc/rehash.html
nodist_pdf_DATA = doc/rehash.pdf
nodist_ps_DATA = doc/rehash.ps

# TODO proper dependency information
# TODO headless texmacs
# TODO package relevant package for xvfb-run in Guix
# FIXME why does setting the geometry not work?
XVFB_RUN =
TEXMACS = texmacs
TEXMACS_CMD = $(XVFB_RUN) $(TEXMACS) -g 1x1 -b "$(top_srcdir)/build-aux/do-nothing.scm"
%.html: %.tm
	$(MKDIR_P) "`dirname "$@"`" && \
	$(TEXMACS_CMD) -c "$<" "$@" -q
%.pdf: %.tm
	$(MKDIR_P) "`dirname "$@"`" && \
	$(TEXMACS_CMD) -c "$<" "$@" -q
%.ps: %.tm
	$(MKDIR_P) "`dirname "$@"`" && \
	$(TEXMACS_CMD) -c "$<" "$@" -q

html-local: doc/rehash.html
pdf-local: doc/rehash.pdf
ps-local: doc/rehash.ps
install-html-local: install-nodist_htmlDATA
install-pdf-local: install-nodist_pdfDATA
install-ps-local: install-nodist_psDATA

